package com.jams.goodox.adapter.lfadapter;

import android.content.Context;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.jams.goodox.R;
import com.jams.goodox.activity.qa.entity.Question;
import com.jams.goodox.utils.TimeUtils;

import java.net.URI;
import java.util.List;


/**
 * Created by HP on 2018/5/19.
 */

public class QaFragmentAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private static final String TAG = QaFragmentAdapter.class.getSimpleName();
    private static final int TYPE_001 = 0;//编辑框
    private static final int TYPE_002 = 1;//按钮
    private Context context;

    private List<Question> list;

    private OnItemClickListener onItemClickListener;



    public interface OnItemClickListener {
        void OnItemClick(int position,boolean isAnswer);
    }

    public void setOnItemClickListener(OnItemClickListener listener){
        this.onItemClickListener = listener;
    }

    public QaFragmentAdapter(Context context, List<Question> list ) {
        this.context = context;
        this.list = list;
    }

    public void setData(List<Question> list){
        this.list = list;
        notifyDataSetChanged();
    }


    @Override
    public int getItemViewType(int position) {
        Log.d("TTTT","type = " + list.get(position).getType());
        return list.get(position).getType() ;

    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

//        if (viewType == 0) {
//            View view = LayoutInflater.from(context).inflate(R.layout.homefragment_text, parent, false);
//            TextHolder viewHolder = new TextHolder(view);
//            return viewHolder;
//        }

//        if(viewType == 1){
            View v = LayoutInflater.from(context).inflate(R.layout.question_item,   parent,false);
            QuestionViewHolder th = new QuestionViewHolder(v);
            return th;
//        }


    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
        final Question question = list.get(position);

//        if(holder instanceof TextHolder){
//            if(!question.isAnswer()){
//                ((TextHolder) holder).title.setText("未回答");
//            }else {
//                ((TextHolder) holder).title.setText("已回答");
//            }
//        }

//        if(holder instanceof  QuestionViewHolder){

            ((QuestionViewHolder) holder).nickName.setText(question.getNickName());
            ((QuestionViewHolder) holder).conent.setText(question.getDescr());
            ((QuestionViewHolder) holder).createTime.setText(TimeUtils.getDateToString(question.getCreateTime()));

            if(question.getQueState() == 0) {
                ((QuestionViewHolder) holder).type.setText("未回答");
            }else {
                ((QuestionViewHolder) holder).type.setText("已回答");
            }


            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onItemClickListener.OnItemClick(position,question.isAnswer());
                }
            });


            ((QuestionViewHolder) holder).imageView.setImageURI(Uri.parse(question.getImgPath()));


//        }



    }

    @Override
    public int getItemCount() {
        return list.isEmpty()? 0 : list.size();
    }



    //显示标题
    class TextHolder extends RecyclerView.ViewHolder{
        public TextView title;

        public TextHolder(View itemView) {
            super(itemView);
            title = (TextView)itemView.findViewById(R.id.title);
        }
    }

    class QuestionViewHolder extends RecyclerView.ViewHolder{
        public TextView conent;
        public TextView nickName;
        public TextView createTime;
        public TextView type;
        public SimpleDraweeView imageView;

        public QuestionViewHolder(View itemView) {
            super(itemView);
            conent = (TextView)itemView.findViewById(R.id.conent);
            nickName = (TextView)itemView.findViewById(R.id.nickName);
            createTime = (TextView)itemView.findViewById(R.id.createTime);
            type = (TextView)itemView.findViewById(R.id.type);
            imageView = (SimpleDraweeView)itemView.findViewById(R.id.logo);
        }
    }
}
